//
//  LevelCompleteScene.m
//  SpeedTap
//
//  Created by Chi Ho Chiu on 28/8/11.
//  Copyright 2011 Individual. All rights reserved.
//

#import "LevelCompleteScene.h"
#import "GameManager.h"

@implementation LevelCompleteScene
@synthesize layer, whiteLayer;

-(id) init
{
    if( (self = [super init]) )
    {       
        [[GameManager sharedGameManager] setGameState:kGAME_STATE_LEVEL_COMPLETED];
        CCLOG(@"GameState: kGAME_STATE_LEVEL_COMPLETED");
     
        [self setLayer: [LevelCompleteLayer node]];
        [layer setDelegate:self];
        [self addChild:layer];
        
        //Add White Layer
        self.whiteLayer = [CCLayerColor layerWithColor:ccc4(255, 255, 255, 255)];
        [self addChild:whiteLayer z:999];
        [self scheduleUpdate];
    }
    
    return self;
}

-(void) update: (ccTime)delta
{
    //Reduce opactiy by 1
    [whiteLayer setOpacity: whiteLayer.opacity - 1];
    
    //Check if opactiy <= 0
    if(whiteLayer.opacity <= 0)
    {
        [self unscheduleUpdate];    //Remove update
        [self removeChild:whiteLayer cleanup:YES];  //Remove whiteLayer
        [whiteLayer release];   //Release whiteLayer
    }
}

-(void) dealloc
{
    [self unscheduleUpdate];    //To ensure all updated stopped
    
    if(whiteLayer != nil)
    {
        [whiteLayer release];
    }
    
    [layer release];
    [super dealloc];
}

-(void) onContinueItemClicked
{
    int iNextLevel = [[GameManager sharedGameManager] currentLevel] + 1;
    [[GameManager sharedGameManager] replaceScene:(EGameScene)iNextLevel];
}

-(void) onRetryItemClicked
{
    int iCurrentLevel = [[GameManager sharedGameManager] currentLevel];
    [[GameManager sharedGameManager] replaceScene:(EGameScene)iCurrentLevel];
}

-(void) onExitItemClicked
{
    [[GameManager sharedGameManager] replaceScene:kGAME_SCENE_MAIN_MENU];
}
@end
